<template>
  <div class="app-container">
    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" :element-loading-text="$t('Searching')" max-height="700" stripe>
      <el-table-column type="expand">
        <template slot-scope="{row}">
          <el-row style="margin:3px;">
            <el-col :span="4">
              <el-tag effect="dark">{{ $t('面额') }}</el-tag>
            </el-col>
            <el-col :span="4">
              <el-tag effect="dark">{{ $t('数量') }}</el-tag>
            </el-col>
          </el-row>
          <el-row v-for="(item, index) in row.denomination" :key="index" style="margin:3px;">
            <el-col :span="4">
              <el-tag effect="dark">{{ item.price }}</el-tag>
            </el-col>
            <el-col :span="20">
              <el-tag effect="dark">{{ item.number }}</el-tag>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column type="index" :index="getIndex" width="50"></el-table-column>
      <el-table-column align="center" :label="$t('商店名称')" prop="shopName" />
      <el-table-column align="center" :label="$t('用户账号')" prop="userName" />
      <el-table-column align="center" :label="$t('钱箱中金额')" prop="totalCash" />
      <el-table-column align="center" :label="$t('钱箱中应有金额')" prop="deserveCash" />
      <el-table-column align="center" :label="$t('添加时间')" prop="addTime" />
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />
  </div>
</template>

<script>
import { calculateSettlementList } from '@/api/shop'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
export default {
  name: 'calculateSettlementList',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        shopId: undefined,
        page: 1,
        limit: 20,
        status: undefined,
        name: undefined,
        address: undefined,
        regionIds: undefined,
        // sort: 'add_time',
        // order: 'desc'
      },
    }
  },
  created() {
    this.listQuery.shopId = this.$route.query.shopId;
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      let param = _.clone(this.listQuery);
      calculateSettlementList(param).then(response => {
        const res = response.data.data;
        res.content.map(item => {
          item.denomination = this.parseObj(item.denomination);
          return item;
        });
        this.list = res.content;
        console.log(this.list);
        this.total = res.totalCount;
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    getIndex(index) {
      return (index + 1) + (this.listQuery.page - 1) * this.listQuery.limit;
    },
    parseObj(data) {
      const target = data.slice(1, data.length - 1);
      let newArr = target.split(', ');
      newArr = newArr.map(item => {
        item = item.replace("}", '').replace('{', '');
        return item;
      })
      var copyArr = [];
      for (let i = 0; i < newArr.length; i += 2) {
        const price = newArr[i].split(': ')[1];
        const number = newArr[i + 1].split(': ')[1];
        copyArr.push({ price: price, number: number });
      }
      return copyArr;
    }
  }
}
</script>

<style scoped>

</style>
